home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / time / adjtimex.2 / adjtimex / adjtimex.c < prev    next >
C/C++ Source or Header  |  1995-03-14  |  4KB  |  165 lines

  1. /*
  2.     adjtimex - display or set the kernel time variables
  3.  
  4.  
  5.     AUTHORS
  6.         ssd@nevets.oau.org (Steven S. Dick)
  7.         jrv@vanzandt.mv.com (Jim Van Zandt)
  8.  
  9.     $Id: adjtimex.c,v 1.2 1995/03/15 01:08:55 jrv Exp jrv $
  10.  
  11.     $Log: adjtimex.c,v $
  12.  * Revision 1.2  1995/03/15  01:08:55  jrv
  13.  * Moved documentation to README file and man page.
  14.  * Ran through indent.
  15.  * Usage msg only shows "print" once.
  16.  *
  17.  * Revision 1.1  1995/03/07  01:46:31  jrv
  18.  * Initial revision
  19.  *
  20.  
  21. ********/
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30. #include <syscall.h>
  31. #include <sys/timex.h>
  32.  
  33. _syscall1(int, adjtimex, struct timex *, txcp)
  34. int F_print = 0;
  35.  
  36. struct timex txc;
  37.  
  38. #define TXO(x) &txc.x
  39. struct options {
  40.     const char *name;
  41.     int minlen;
  42.     int mask;
  43.     long *offset;
  44. }
  45.  
  46. Options[] =
  47. {
  48.     { "print", 1, 0, 0 } ,
  49.     { "offset", 1, ADJ_OFFSET, TXO(offset) } ,
  50.     { "singleshot", 2, ADJ_OFFSET_SINGLESHOT, TXO(offset) } ,
  51.     { "frequency", 1, ADJ_FREQUENCY, TXO(frequency) } ,
  52.     { "maxerror", 1, ADJ_MAXERROR, TXO(maxerror) } ,
  53.     { "esterror", 1, ADJ_ESTERROR, TXO(esterror) } ,
  54.     { "timeconstant", 3, ADJ_TIMECONST, TXO(time_constant) } ,
  55.     { "tick", 3, ADJ_TICK, TXO(tick) } ,
  56.     { "status", 2, ADJ_STATUS, (long *) TXO(status) } ,
  57.     { 0, 0, 0, 0 }
  58. };
  59.  
  60. void
  61. usage(void)
  62. {
  63.     struct options *op;
  64.  
  65.     fprintf(stderr, "Usage:\n\tadjtimex [-print] [-option newvalue]\n"
  66.         "Where option is one of:\n\t");
  67.     for (op = Options; (++op)->name;)
  68.     fprintf(stderr, "%s ", op->name);
  69.     fprintf(stderr, "\n");
  70.     exit(1);
  71. }
  72.  
  73. int
  74. main(int argc, char *argv[])
  75. {
  76.     int ret, saveerr;
  77.     unsigned int len;
  78.     long num;
  79.  
  80.     txc.mode = 0;
  81.  
  82.     {
  83.     char **ap, *s;
  84.     struct options *op;
  85.  
  86.     for (ap = argv + 1; *ap; ap++) {
  87.         if (**ap != '-')
  88.         usage();
  89.         s = *ap + 1;
  90.         len = strlen(s);
  91.         for (op = Options; op->name; op++) {
  92.         if (strncmp(s, op->name, len) == 0) {
  93.             if (len < op->minlen) {
  94.             fprintf(stderr, "Option '%s' not unique, "
  95.             "needs at least %d letters\n", s, op->minlen);
  96.             usage();
  97.             } else if (!op->offset) {    /* no args */
  98.             if (ap[1] && ap[1][0] != '-') {
  99.                 fprintf(stderr, "Option '%s' does not take "
  100.                             "arguments.\n", op->name);
  101.                 usage();
  102.             }
  103.             /* only non-arg option is print right now */
  104.             F_print = 1;
  105.             } else {
  106.             if (!*++ap || sscanf(*ap, "%ld", &num) != 1) {
  107.                 fprintf(stderr, "Option '%s' requires a value.\n", 
  108.                                     op->name);
  109.                 usage();
  110.             } else {
  111.                 if (op->mask == ADJ_STATUS)
  112.                 *(int *) op->offset = num;
  113.                 else
  114.                 *op->offset = num;
  115.                 txc.mode |= op->mask;
  116.             }
  117.             }
  118.             break;
  119.         }
  120.         }
  121.         if (!op->name) {
  122.         fprintf(stderr, "Unknown option '%s'.\n", s);
  123.         usage();
  124.         }
  125.     }
  126.     }
  127.     ret = adjtimex(&txc);
  128.     saveerr = errno;
  129.     if (F_print) {
  130.     printf("         mode: %d\n"
  131.            "       offset: %ld\n"
  132.            "    frequency: %ld\n"
  133.            "     maxerror: %ld\n"
  134.            "     esterror: %ld\n"
  135.            "       status: %d\n"
  136.            "time_constant: %ld\n"
  137.            "    precision: %ld\n"
  138.            "    tolerance: %ld\n"
  139.            "         tick: %ld\n"
  140.            "         time:  %lds %ldus\n",
  141.            txc.mode,
  142.            txc.offset,
  143.            txc.frequency,
  144.            txc.maxerror,
  145.            txc.esterror,
  146.            txc.status,
  147.            txc.time_constant,
  148.            txc.precision,
  149.            txc.tolerance,
  150.            txc.tick,
  151.            txc.time.tv_sec, 
  152.            txc.time.tv_usec);
  153.     if (saveerr == 0 && ret != 0)
  154.         printf(" return value = %d\n", ret);
  155.     }
  156.     if (ret != 0 && saveerr != 0) {
  157.     if (ret != -1)
  158.         fprintf(stderr, "%d ", ret);
  159.     errno = saveerr;
  160.     perror("adjtimex");
  161.     exit(1);
  162.     }
  163.     return 0;
  164. }
  165.